package com.peng.leetcode.tree;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * CountNodes1
 * 222. 完全二叉树的节点个数
 * https://leetcode.cn/problems/count-complete-tree-nodes/
 * <p>
 * Created on 2022/12/2
 *
 * @author lupeng
 */
public class CountNodes1 {
    public int countNodes(TreeNode root) {
        int ans = 0;
        if (root == null) {
            return ans;
        }
        Queue<TreeNode> q = new ArrayDeque<>();
        q.offer(root);
        while (!q.isEmpty()) {
            TreeNode p = q.poll();
            ans++;
            if (p.left != null) {
                q.offer(p.left);
            }
            if (p.right != null) {
                q.offer(p.right);
            }
        }
        return ans;
    }
}
